package com.stedu.mall.adminservice.controller;

import cn.hutool.captcha.LineCaptcha;
import cn.hutool.core.util.IdUtil;
import com.github.pagehelper.PageInfo;
import com.stedu.mall.common.bean.Admin;
import com.stedu.mall.common.bean.RespBean;
import com.stedu.mall.common.exception.SteduException;
import com.stedu.mall.common.service.AdminService;
import com.stedu.mall.common.utils.JwtUtil;
import com.stedu.mall.common.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private AdminService adminService;

    //添加
    @PostMapping("/insert")
    public RespBean reg(@RequestBody  Admin admin) throws SteduException {
        adminService.insert(admin);
        return RespBean.ok("添加成功");
    }


    @GetMapping("/captcha")
    public RespBean captcha(){
        LineCaptcha captcha=new LineCaptcha(120,38,4,10 );

        String key= IdUtil.getSnowflakeNextIdStr();

        redisUtil.set(key, captcha.getCode(), 60);

        String imageBase64Data=captcha.getImageBase64Data();

        HashMap<String,Object> map=new HashMap<>();
        map.put("key", key);
        map.put("imageBase64Data", imageBase64Data);
        return  RespBean.ok("生成验证码成功",map);
    }

    @PostMapping("/login")
    public RespBean login(String username,String password,String key,String captchaInput) throws SteduException {
        //判断验证码是否正确-从Redis中查询验证码进行判断
       String captcha =(String)redisUtil.get(key);
        if(captcha == null || !captcha.equalsIgnoreCase(captchaInput)){
            throw new SteduException("验证码错误");
        }
        Admin admin=adminService.longin(username, password);
    HashMap<String,Object>map=new HashMap<>();
        map.put("id",admin.getId());
        map.put("username",admin.getUsername());
        map.put("role","admin");
        String jwtStr= JwtUtil.generateJwt(map);

        //登录
        //生成token
        //返回token给用户a
return RespBean.ok("登录成功",jwtStr);
    }

@GetMapping("/info")
    public RespBean getLogin(@RequestHeader("token") String token){
        //token
    Map<String, Object> map = JwtUtil.parseJwtToMap(token);
    //
    Integer id = (Integer)map.get("id");
    Admin admin= adminService.selectById(id);
    return RespBean.ok("",admin);

}
//修改密码
    @PutMapping("/chgPwd")
    public  RespBean chgPwd(String oldPwd, String newPwd, String newPwd1 ,@RequestHeader("token") String token) throws SteduException {
    if(!newPwd.equals(newPwd1)){
        throw new SteduException("俩次输入的密码不一样,无法修改");
    }
        Map<String, Object> map = JwtUtil.parseJwtToMap(token);
        Integer id = (Integer) map.get("id");
        adminService.chgPwd(oldPwd,newPwd,id);


        return RespBean.ok("修改密码成功");
    }

    //修改管理员信息
    @PutMapping("/chgInfo")
    public RespBean chgInfo(@RequestBody Admin admin)  {
        admin.setUsername(null);
        adminService.update(admin);
        return RespBean.ok("修改个人信息成功");
    }
    @GetMapping("/All")
    public RespBean selectByPage(Admin condition, Integer pageNum, Integer pageSize){
        pageNum = pageNum==null ? 1:pageNum;
        pageSize = pageSize==null ? 5:pageSize;
        PageInfo<Admin> pageInfo =adminService.selectByCondition(condition, pageNum, pageSize);
        return RespBean.ok("查询成功",pageInfo);
    }
    @PutMapping("/updateSelect")
    public RespBean updateSelect(Integer id, Integer status){
        Admin admin = adminService.selectById(id);
        admin.setStatus(status);
        adminService.update(admin);
        return  RespBean.ok("修改成功");
    }
    @PutMapping("/{id}")
    public RespBean updateStatus(@PathVariable("id") Integer id)  {
    Admin admin=adminService.selectById(id);
    if(admin.getStatus().equals(0)){
        admin.setStatus(1);
    }else { admin.setStatus(0);}
    adminService.update(admin);
    return RespBean.ok("修改成功");
    }
    @PutMapping("/pwd/{id}")
    public RespBean updatePwd( @PathVariable("id") Integer id)  {
        Admin admin=adminService.selectById(id);
         admin.setPassword("000000");
        adminService.updatePwd(admin);
        return RespBean.ok("重置成功");
    }
}
